package io.renren.modules.learn.controller;

import io.renren.common.utils.R;
import io.renren.modules.learn.entity.AlgorithmChapterEntity;
import io.renren.modules.learn.entity.AlgorithmKnowledgeEntity;
import io.renren.modules.learn.service.AlgorithmChapterService;
import io.renren.modules.learn.service.AlgorithmKnowledgeService;
import io.renren.modules.learn.service.StudentProgressService;
import io.renren.modules.storage.LoginUserLocalStorage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/learn/algorithm-new")
@Api("算法学习分章节接口")
public class LearnAlgorithmNewController {

    @Autowired
    private AlgorithmChapterService algorithmChapterService;

    @Autowired
    private AlgorithmKnowledgeService algorithmKnowledgeService;

    @Autowired
    private StudentProgressService studentProgressService;

    /**
     * 获取所有算法学习章节
     */
    @ApiOperation(value = "获取所有算法学习章节", notes = "返回章节列表")
    @GetMapping("/chapters")
    public R getAllChapters() {
        List<AlgorithmChapterEntity> chapters = algorithmChapterService.getAllChapters();
        return R.ok().put("data", chapters);
    }

    /**
     * 根据章节ID获取该章节下的所有知识点
     */
    @ApiOperation(value = "根据章节ID获取知识点", notes = "返回知识点列表")
    @GetMapping("/chapters/{chapterId}/knowledge")
    public R getKnowledgeByChapterId(@PathVariable("chapterId") Integer chapterId) {
        List<AlgorithmKnowledgeEntity> knowledgeList = algorithmKnowledgeService.getByChapterId(chapterId);
        return R.ok().put("data", knowledgeList);
    }

    /**
     * 根据知识点ID获取知识点详情
     */
    @ApiOperation(value = "根据知识点ID获取详情", notes = "返回知识点详情")
    @GetMapping("/knowledge/{knowledgeId}")
    public R getKnowledgeById(@PathVariable("knowledgeId") Integer knowledgeId) {
        AlgorithmKnowledgeEntity knowledge = algorithmKnowledgeService.getById(knowledgeId);
        return R.ok().put("data", knowledge);
    }

    /**
     * 标记某个算法章节知识点状态是已经完成学习
     * 保留原有的"我学会了"逻辑
     */
    @PostMapping("/chapter/{chapterId}/learned")
    public R setLearningProgress(@PathVariable("chapterId") int chapterId) {
        Long userId = LoginUserLocalStorage.getCurrentLoginUserId();
        studentProgressService.setAlgorithmLearningProgress(userId, chapterId);
        return R.ok();
    }
} 